package zhklappoint.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import zhklappoint.annotation.IgnoreAuth;
import zhklappoint.entity.FunctionAndCompany;
import zhklappoint.entity.FunctionKinds;
import zhklappoint.enums.ENUM_FUNCTION_STATUS;
import zhklappoint.enums.ENUM_ISDELETE;
import zhklappoint.enums.ENUM_ISUSING;
import zhklappoint.enums.ENUM_SUCESS;
import zhklappoint.service.FunctionAndCompanyService;
import zhklappoint.service.FunctionKindsService;
import zhklappoint.util.ResponseBean;

/***
 * 后台接口
 * @author eagle
 *
 */
@Controller
@CrossOrigin
@RequestMapping(value="/uni/api",produces = "text/html;charset=UTF-8")
public class FunctionKindsController {
	
	
	@Autowired
	private FunctionKindsService fks;
	@Autowired
	private FunctionAndCompanyService facs;
	
	@IgnoreAuth
	@RequestMapping(value="/ceshi",produces = "application/x-www-form-urlencoded;charset=UTF-8")
	@ResponseBody
	public String ceshi() {
		System.out.println("接口进来了");
		return "接口访问成功";
	}
	@IgnoreAuth
	@RequestMapping(value="/getFunctionKinds",produces = "application/x-www-form-urlencoded;charset=UTF-8")
	@ResponseBody
	public String getFunctionKinds(String shopids) {
		Page<FunctionKinds> pfks = fks.getCompanyHasFuncion(shopids, 1, 100);
		return ResponseBean.of(pfks.getContent(), ENUM_SUCESS.SUCESS.value, "获取模块成功");
	}

	@RequestMapping(value="/getAllFunctionKinds",produces = "application/x-www-form-urlencoded;charset=UTF-8")
	@ResponseBody
	public String getAllFunctionKinds(String comId) {
		FunctionKinds fk = new FunctionKinds();
		fk.setStatus(ENUM_FUNCTION_STATUS.FREE.value);
		Page<FunctionKinds> funcs = fks.getFunctionKindsList(fk, 1, 100);
		if(!ObjectUtils.isEmpty(comId)) {
			FunctionAndCompany fkCom = new FunctionAndCompany();
			fkCom.setCompanyId(comId);
			Page<FunctionAndCompany> fcoms = facs.getFunctionAndCompanyObj(fkCom, 1, 100);
			for(FunctionKinds k:funcs) {
				k.setIson(false);
				for(FunctionAndCompany faccc:fcoms.getContent()) {
					if(faccc.getFkId().equals(k.getId())) {
						k.setIson(true);
						break;
					}
				}
			}
			return ResponseBean.of(funcs, ENUM_SUCESS.SUCESS.value, "获取模块成功");
		}else {
			return ResponseBean.of(null, ENUM_SUCESS.FAILED.value, "获取功能模块出错");
		}
		
	}
	
	@RequestMapping(value="/saveComFunctionKinds",produces = "application/x-www-form-urlencoded;charset=UTF-8")
	@ResponseBody
	public String saveComFunctionKinds(String fkList,String comId) {
		JSONArray ja = JSON.parseArray(fkList);
		for(int i=0;i<ja.size();i++) {
			FunctionAndCompany fkCom = new FunctionAndCompany();
			fkCom.setCompanyId(comId);
			Map<String,Object> mapType = JSON.parseObject(ja.get(i)+"",Map.class);
			fkCom.setFkId(mapType.get("id")+"");
			Page<FunctionAndCompany> fcoms = facs.getFunctionAndCompanyObj(fkCom, 1, 10);
			if(fcoms.getTotalElements()==0) {
				FunctionAndCompany fkC = new FunctionAndCompany();
				fkC.setCompanyId(comId);
				fkC.setFkId(mapType.get("id")+"");
				fkC.setStatus(ENUM_ISUSING.USING.value);
				facs.saveFunctionAndCompanyObj(fkC);
			}else {
				FunctionAndCompany fkCom1 = fcoms.getContent().get(0);
				fkCom1.setIsdelete(ENUM_ISDELETE.DELETED.value);
				facs.saveFunctionAndCompanyObj(fkCom1);
			}
		}
		return ResponseBean.of(null, ENUM_SUCESS.SUCESS.value, "获取模块成功");
	}
	
	
	//下方为付费的模块先把免费的搞完了 来搞钱  TODO 
	@RequestMapping(value="/getPayFunctionKinds",produces = "application/x-www-form-urlencoded;charset=UTF-8")
	@ResponseBody
	public String getPayFunctionKinds(String comId) {
		FunctionKinds  f= new FunctionKinds();
		f.setStatus(ENUM_FUNCTION_STATUS.PAID.value);
		Page<FunctionKinds> pfkslist = fks.getFunctionKindsList(f, 1, 100);
		List<FunctionKinds> pfks=pfkslist.getContent();
		FunctionAndCompany fkCom = new FunctionAndCompany();
		fkCom.setCompanyId(comId);
		Page<FunctionAndCompany> fcoms = facs.getFunctionAndCompanyObj(fkCom, 1, 100);
		for(FunctionKinds fk:pfks) {
			fk.setIson(false);
			for(FunctionAndCompany faccc:fcoms.getContent()) {
				if(faccc.getFkId().equals(fk.getId())) {
					fk.setIson(true);
					break;
				}
			}
		}
		return ResponseBean.of(pfks, ENUM_SUCESS.SUCESS.value, "获取模块成功");
	}
}
